/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package koala.objects;

import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.io.*;
import java.util.*;

public class PasswordFunction {
    
    public PasswordFunction(){}
    
    private String pass;
    private String user;
    
    public String getUser(){
        return user;
    }
    
    public void setUser(String user){
        this.user = user;
    }
 
    public String getPassword(){
        return pass;
    }
    
    public void setPassword(String pass){
        this.pass = pass;
    }
    
    public Connection establishConnection() throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        String conStr = "jdbc:mysql://127.0.0.1:3306/koalaproject?user=root&password=";

        Connection con = DriverManager.getConnection(conStr);
        System.out.println("connection done");

        return con;
    }
    
    public byte[] getSalt() throws NoSuchAlgorithmException, Exception {
        PasswordEncryptionService serv = new PasswordEncryptionService();
        
        return serv.generateSalt();
    }
    
    
    public void saveAndEncryptPassword(String user, String password) throws Exception{
        PasswordEncryptionService service = new PasswordEncryptionService();
        Connection con = establishConnection();
        
        ArrayList<User> users = new DatabaseConnection().userList();
        
        byte[] salt = getSalt();
        byte[] hashedPass = service.getEncryptedPassword(password, salt);
        
        PreparedStatement psi;
        String stIns = "INSERT INTO koalaproject.usercredential(userID, password, salt) VALUES (?,?,?)";
        psi = con.prepareStatement(stIns);
        for(User in : users){
            if(user.equals(in.getUserId())){
                psi.setString(1, user);
            }
        }
        psi.setBytes(2, hashedPass);
        psi.setBytes(3, salt);
        psi.executeUpdate();
    }
    
    public String encryptPassword(String password, String slt) throws Exception{
        PasswordEncryptionService service = new PasswordEncryptionService();
        
        byte[] salt = getSalt();
        byte[] hashedPass = service.getEncryptedPassword(password, salt);
        
        return hashedPass.toString();
    }
    
    public byte[] retrievePassword(String user) throws Exception{
        Connection con = establishConnection();
        
        
        String stSel = "SELECT usercredential.password FROM koalaproject.usercredential\n" + "WHERE userID =" + "'" + user +"'" + ";";
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery(stSel);
        
        rs.beforeFirst();
        
        while (rs.next()) {
            //pswd = rs.getString("password");
            //System.out.println(pswd);
            try{
                // Get as a BLOB
                Blob aBlob = rs.getBlob("password");
                byte[] passwd = aBlob.getBytes(1, (int) aBlob.length());
                
                aBlob.free();
                
                return passwd;
            }catch(Exception ex){
            }
        }
        
        return  rs.getBytes("password");
    }
    
    public byte[] retrieveSalt(String user) throws Exception{
        Connection con = establishConnection();
        
        String stSel = "SELECT usercredential.salt FROM koalaproject.usercredential\n" + "WHERE userID =" + "'" + user +"'" + ";";
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery(stSel);
        
        rs.beforeFirst();
        
        while (rs.next()) {
            //salt = rs.getString("salt");
            //System.out.println(salt);
            try{
                // Get as a BLOB
                Blob aBlob = rs.getBlob("salt");
                byte[] salt = aBlob.getBytes(1, (int) aBlob.length());
                
                aBlob.free();
                
                return salt;
            }catch(Exception ex){
            }
            
        }
        
        return  rs.getBytes("salt");
    }
    
}
